<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\BaseController;
use app\common\lib\Show;
use think\facade\Request;
use app\common\business\Address as BusiAddress;

class Address extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $condition = ['uid' => Request::header('userId')];
        try {
            $address = new BusiAddress();
            $data = $address->getLists($condition);
        } catch (\Exception $e) {
            return Show::error();
        }
        return $data ? Show::success($data) : Show::error();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $data = input('param.');
        $data['uid'] = Request::header('userId');

        $validate = new \app\api\validate\Address();
        if (!$validate->check($data)) {
            return Show::error($validate->getError());
        }

        try {
            $address = new BusiAddress();
            $result  = $address->add($data);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }

        return $result ? Show::success([], '地址添加成功') : Show::error("地址添加失败");
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read(int $id)
    {
        if (!$id) return Show::error('参数错误');

        try {
            $busiAddress = new BusiAddress();
            $result      = $busiAddress->getNormalById($id);
        } catch (\Exception $e) {
            return Show::error();
        }
        return $result ? Show::success($result) : Show::error();
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(int $id)
    {
        $data = input('param.');
        $data['uid'] = Request::header('userId');

        $validate = new \app\api\validate\Address();
        if (!$validate->check($data)) {
            return Show::error($validate->getError());
        }

        try {
            $busiAddress = new BusiAddress();
            $update      = $busiAddress->updateAddressById($id, $data);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }

        return $update ? Show::success([], '地址修改成功') : Show::error('地址修改失败');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(int $id)
    {
        $busiAddress = new BusiAddress();
        $delete      = $busiAddress->deleteDataById($id);
        return $delete ? Show::success([], '删除成功') : Show::error('删除失败');
    }

    /**
     * 用户默认地址修改
     *
     * @param int $id
     * @return array
     */
    public function updateDefault(int $id)
    {
        $userId = (int) Request::header('userId');
        try {
            $busiAddress = new BusiAddress();
            $update      = $busiAddress->updateDefaultAddressByUserid($userId, $id);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }
        return $update ? Show::success([], '默认地址修改成功') : Show::error('默认地址修改失败');
    }
}
